package com.app.exception;

import com.app.framework.common.R;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestHeaderException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.util.List;

/**
 * 处理异常信息
 */
@Slf4j
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    /**
     * 接口方法参数类型异常
     */
    @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
    @ResponseBody
    public Object methodArgumentTypeMismatchException(Throwable e) {
        log.error("url参数异常，请检查参数类型是否匹配！", e);
        return R.error("请检查请求参数类型是否正确");
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public R<?> systemException(Exception e) {
        log.error(e.getMessage(), e);
        return R.error("系统繁忙,请稍后再试!");
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(HttpMediaTypeException.class)
    public R<?> httpException(HttpMediaTypeException e) {
        log.error("请求异常" + e.getMessage(), e);
        return R.error("系统繁忙,请稍后再试!");
    }

    /**
     * 业务异常
     */
    @ExceptionHandler(RuntimeException.class)
    public R<?> businessExceptionHandler(RuntimeException e) {
        log.error(e.getMessage(), e);
        return R.error(e.getMessage());
    }

    /**
     * 调用web异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public R<?> illegalArgumentExceptionHandler(IllegalArgumentException e) {
        //log.error(e.getMessage(), e);
        //log.error("异常处理##{}", e.getMessage());
        return R.error(e.getMessage());
    }

    /**
     * 入参异常
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public R<String> httpMessageNotReadableExceptionHandler(HttpMessageNotReadableException e) {
        //log.error("请求参数错误：{}", e.getMessage());
        return R.error("请求参数错误");
    }

    /**
     * javax.validation 校验拦截
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        //log.error("MethodArgumentNotValidException参数异常" + e.getMessage(), e);
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        for (FieldError error : fieldErrors) {
            return R.error(error.getDefaultMessage());
        }
        return R.error("参数错误");
    }

    /**
     * javax.validation 校验拦截
     */
    @ExceptionHandler(BindException.class)
    public R<String> bindExceptionHandler(BindException e) {
        //log.error("BindException参数异常", e);
        return R.error(e.getMessage());
    }

    /**
     * 请求类型错误拦截
     */
    @ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
    public R<?> handleMediaTypeNotAcceptableException(HttpServletRequest request, Exception e, HttpMediaTypeNotAcceptableException ex) {
        //log.error("请求的媒体类型不被接受", e);
        return R.error("请求的媒体类型不被接受");
    }

    /**
     * 请求头缺少token异常
     */
    @ExceptionHandler(MissingRequestHeaderException.class)
    public R<?> missingTokenExceptionHandler(MissingRequestHeaderException e) {
        //log.error("请求头缺少token", e);
        return R.error("token不存在");
    }

}